<?php
	set_include_path (PHPPATH);
	require_once 'MDB2.php';
	/**
	 * 
	 * The auth controller
	 * @author Tri
	 *
	 */
	class auth extends Application 
	{
		/**
		 * 
		 * Constructor
		 */
		function __construct(&$tmpl)
		{			
			$this->tmpl = &$tmpl;		
		}

		/**
		 * 
		 * Default action
		 */
		function index()
		{	
			
		}
		
		/**
		 * Logout 
		 * 
		 */
		function logout()
		{
			session_destroy();
			$this->sessionDefaults();	
	
			// about button visible
			$this->assign("aboutVisible", 1);
			// search button visible
			$this->assign("searchVisible", 1);
			
			//processing		
			$typeModel = $this->getModel('TypeModel');
			$types = $typeModel->getAllTypes();
			$typePath = $this->loadResources('type');
			//output
			$this->assign("types", $types);
			$this->assign("typesCount", count($types));
			$this->assign("imagesPath", $this->loadResources('images'));
			$this->assign("typePath", $typePath);
			$this->assign("title", "StylePad");
			$this->assign("currentView", "home");
			$this->loadTemplate('homepage');	
		}
		
		/**
		 * Login
		 * 
		 */
		function login()
		{
			if ($_SERVER['REQUEST_METHOD'] == 'GET')
			{
				$currentView = $_GET['view'];
				$this->assign("currentView", $currentView);
				$backTitle = $_GET['back'];
				$this->assign("backTitle", $backTitle);
				$this->assign("title", "Login");
				$this->assign("page", "login");
				
				$_SESSION['sid'] = session_id();
				
				$this->loadPageTemplate('login');
			} 
			else if ($_SERVER['REQUEST_METHOD'] == 'POST')
			{
				$username = $_POST['username'];
				$password = $_POST['password'];
				$usersModel = $this->getModel('UserModel');
	            $user = $usersModel->login($username,$password);
	            	            
	            if($user == false){
	                echo "false";
	            }else{
	            	session_unset(); //destroys variables
   					session_destroy(); //destroys session
	            	session_id($_POST['sid']);
	            	session_start();
	            	
	            	$this->setSessionValue("uid", $user['uid']);
					$this->setSessionValue("username", $user['name']);
					$this->setSessionValue("email", $user['mail']);
					$this->setSessionValue("logged", true);
					$this->setSessionValue("cookie", 0);
					$this->setSessionValue("remember", false);

					echo 'true';
	            }          
	            return;
			}
		}
		
		/**
		 * 
		 * Register
		 */
		function register()
		{
			if ($_SERVER['REQUEST_METHOD'] == 'GET')
			{
				$currentView = $_GET['view'];
				$this->assign("currentView", $currentView);
				$backTitle = $_GET['back'];
				$this->assign("backTitle", $backTitle);
				$this->assign("title", "Register");
				$this->assign("page", "register");
				
				$this->loadPageTemplate('register');
			} 
			else if ($_SERVER['REQUEST_METHOD'] == 'POST')
			{
				$username = $_POST['username'];
	            $password = $_POST['password'];
	            $email = $_POST['email'];
	            
	            $userModel = $this->getModel('UserModel');

	            if($userModel->checkUsernameExist($username)){
	                echo '0';
	            }
	            else if ($userModel->checkEmailExist($email)){
	            	echo "1";
	            }
	            else{
	                $user = $userModel->register($username, $password, $email);
	                
	                session_unset();
   					session_destroy();
	            	session_id($_POST['sid']);
	            	session_start();
	            	
	            	$this->setSessionValue("uid", $user['uid']);
					$this->setSessionValue("username", $user['name']);
					$this->setSessionValue("email", $user['mail']);
					$this->setSessionValue("logged", true);
					$this->setSessionValue("cookie", 0);
					$this->setSessionValue("remember", false);
	                echo '2';
	            }
			}
		}
	}
?>